package cn.itsource.pethome.basic.service.impl;

import cn.itsource.pethome.basic.mapper.BaseMapper;
import cn.itsource.pethome.basic.query.BaseQuery;
import cn.itsource.pethome.basic.service.IBaseService;
import cn.itsource.pethome.basic.util.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T>{

    @Autowired
    BaseMapper<T> baseMapper;

    @Override
    @Transactional
    public void save(T t) {
        baseMapper.save(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }
    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);
    }

    @Override
    public List<T> findAll() {
        return baseMapper.findAll();
    }

    @Override
    public T findById(Long id) {
        return baseMapper.findById(id);
    }

    @Override
    public PageInfo<T> findPageByQuery(BaseQuery baseQuery) {
        //1.查出部门数据总条数
        Long total = baseMapper.findCountByQuery(baseQuery);
        //2.判断是否为0条 若为0条则没有必要再查分页列表
        if(total.equals(0L)){
            return new PageInfo<T>();
        }
        //查询分页列表
        List<T> list = baseMapper.findPageByQuery(baseQuery);
        //把总条数和分页列表，封装成PageInfo对象
        return new PageInfo<T>(list, total);

    }
    //批量删除
    @Override
    @Transactional
    public void batchRemove(List<T> ts) {
        baseMapper.batchRemove(ts);

    }




}
